//输出字符画
console.log("\n ____       _   _                  \n|  _ \\     | | | |                 \n| |_) | ___| |_| |_ ___ _ __       \n|  _ < / _ \\ __| __/ _ \\ '__|      \n| |_) |  __/ |_| ||  __/ |         \n|____/ \\___|\\__|\\__\\___|_|   _     \n| \\ | |          |__   __|  | |    \n|  \\| | _____      _| | __ _| |__  \n| . ` |/ _ \\ \\ /\\ / / |/ _` | '_ \\\n| |\\  |  __/\\ V  V /| | (_| | |_) |\n|_| \\_|\\___| \\_/\\_/ |_|\\__,_|_.__/ \n\nby zengyl\n");

function textIn(text) {
    if (text != null) {
        return text + ' ';
    } else {
        return '';
    }
}

function htmlEscape(sHtml) {
 return sHtml.replace(/[<>&"]/g,function(c){return {'<':'&lt;','>':'&gt;','&':'&amp;','"':'&quot;'}[c];});
}

//输出日期
function setDate() {
    if (!document.hidden) {
        var Cal = calendar.solar2lunar();
        document.getElementById("cN").innerHTML =
            Cal.gzYear + Cal.Animal + "年 " +
            Cal.IMonthCn + Cal.IDayCn + "<br>" +
            textIn(Cal.lunarFestival) + textIn(Cal.festival) + textIn(Cal.Term);
        document.getElementById("cY").innerHTML = Cal.cYear;
        document.getElementById("cM").innerHTML = Cal.cMonth;
        document.getElementById("cD").innerHTML = Cal.cDay;
        document.getElementById("cW").innerHTML = Cal.ncWeek;
    }
}

//输出时间
function setTime() {
    if (!document.hidden) {
        var Tm = new Date();
        document.getElementById("clH").innerHTML = Tm.getHours().toString().padStart(2, '0');
        document.getElementById("clM").innerHTML = Tm.getMinutes().toString().padStart(2, '0');
        var bless = (
            (Tm.getHours() <= 1 || Tm.getHours() >= 23) ? "深夜" :
            (Tm.getHours() <= 5) ? "凌晨" :
            (Tm.getHours() <= 8) ? "早上" :
            (Tm.getHours() <= 10) ? "上午" :
            (Tm.getHours() <= 13) ? "中午" :
            (Tm.getHours() <= 16) ? "下午" :
            (Tm.getHours() <= 18) ? "傍晚" :
            "晚上"
        ) + "好。";
        document.getElementById("bless").innerHTML = bless;
    }
}

//设置亮/暗模式
var cMode;
var color = {
    dark:`
        :root {
        	--blkbg: linear-gradient(to bottom, #302F2D, #292826) ;
        	--bg: #060503;
        	--shadow: 64, 64, 64;
        	--color: #CFB01E;
        	--color2: #CAAC70;
        	--color3: #A6A5A2;
        	--color4: #DAD9D6;
            --sctrack: #1F1C1A;
            --scthumb: #767370;
        }`,
    light:`
        :root {
            --blkbg: linear-gradient(to bottom, #F6F9FF, #F0F3F9) ;
            --bg: #E3E6EA;
            --shadow: 32, 32, 32;
            --color: #D34329;
            --color2: #AC5644;
            --color3: #666360;
            --color4: #001122;
            --sctrack: #D9DAE3;
            --scthumb: #B0B3BA;
        }`
}
function setColor() {
    var Tm = new Date();
    if ((cMode == "moon") || ((cMode != "sun") && (Tm.getHours() > 18 || Tm.getHours() < 6))) {
        document.getElementById("color").innerHTML = color.dark;
    } else {
        document.getElementById("color").innerHTML = color.light;
    }
}
function clkClBtn() {
    if(cMode == "clock")
        cMode = "sun";
    else if(cMode == "sun")
        cMode = "moon";
    else 
        cMode = "clock";

    document.getElementById("clBtnImg").setAttribute("src", "/image/" + cMode + ".png");
    setColor();
    let s = {btcMode:""};
    s.btcMode = cMode;
    chrome.storage.sync.set(s, function(){});
}

//点击文件夹
function dir(ID) {
    var e = document.getElementById(ID);
    if (e.style.display == "none") {
        e.style.display = "block";
    } else {
        e.style.display = "none";
    }
}

//显示文件列表
function setList(list, ID) {
    var listE = document.getElementById(ID);
    listE.classList.add("listbody");
    for (let i=0; i<list.length; i++) {
        if (list[i].mode == "page") {
            let url = list[i].url;
            let name = list[i].name;
            if(!name) {
                name = url;
            }
            let itemE = document.createElement("a");
            itemE.setAttribute("href", url);
            itemE.innerHTML =
                "<div class=\"listitem\" title=\"" +
                htmlEscape(name) + '\n' + htmlEscape(url) + "\n\n点击以打开链接" +
                "\"><img src=\"chrome://favicon/" + encodeURI(url) +
                "\"><span>" + htmlEscape(name) +
                "</span></div>";
            listE.appendChild(itemE);
        } else {
            let cID = ID + '_' + i;
            let name = list[i].name;
            let itemE = document.createElement("div");
            itemE.classList.add("listitem");
            itemE.classList.add("dir");
            itemE.setAttribute("title", name + "\n\n点击以展开/收起文件夹");
            itemE.addEventListener("click", function () {
                dir(cID);
            });
            itemE.innerHTML = "<img src=\"/image/dir.png\"><span>" + htmlEscape(name) + "</span></div>";
            listE.appendChild(itemE);
            let CE = document.createElement("div");
            CE.setAttribute("id", cID);
            CE.style.setProperty('display', 'none');
            listE.appendChild(CE);
            setList(list[i].children, cID);
        }
    }
}

//输出所有页面
function setPages() {
    chrome.topSites.get(function (fv) {
        var fvL = [];
        for (let i = 0; i < fv.length; i++) {
            let fvJ = {};
            fvJ.mode = "page";
            fvJ.url = fv[i].url;
            fvJ.name = fv[i].title;
            fvL.push(fvJ);
        }
        setList(fvL, "fv");
    });
    chrome.bookmarks.getTree(function (BM) {
        var BMhandle = function (bm) {
            let bmL = [];
            for (let i=0; i<bm.length; i++) {
                let bmJ = {};
                bmJ.name = bm[i].title;
                if (bm[i].url) {
                    bmJ.mode = "page";
                    bmJ.url = bm[i].url;
                } else {
                    bmJ.mode = "dir";
                    bmJ.children = BMhandle(bm[i].children);
                }
                bmL.push(bmJ);
            }
            return bmL;
        };
        var bmL = BMhandle(BM[0].children[0].children);
        setList(bmL, "bm");
    });
    chrome.history.search({
        text: '',
        maxResults: 50,
        startTime: 0
    }, function (hs) {
        var hsL = [];
        var hsLc = [];
        let ld;
        let td = '';
        let nd = domain.getDomain(hs[0].url);
        for (let i=0; i<hs.length; i++) {
            let hsJ = {mode:"page"};
            hsJ.url = hs[i].url;
            hsJ.name = hs[i].title;

            ld = td;
            td = nd;
            if(i < hs.length-2) {
                nd = domain.getDomain(hs[i+1].url);
            }else {
                nd = '';
            }

            if(ld == td) {
                hsLc.push(hsJ);
            }else {
                if(hsLc.length > 0) {
                    let hsJL = {mode:"dir"};
                    hsJL.children = hsLc;
                    hsJL.name = ld;
                    hsL.push(hsJL);
                    hsLc = [];
                }
                if(nd == td) {
                    hsLc.push(hsJ);
                }else {
                    hsL.push(hsJ);
                }
            }
        }
            setList(hsL, "hs");
    });

}

chrome.storage.sync.get({btcMode:"clock"}, function(m) {
    cMode = m.btcMode;
    document.getElementById("clBtnImg").setAttribute("src", "/image/" + cMode + ".png");
    setColor();
});
document.getElementsByClassName("colorBtn")[0].addEventListener("click",clkClBtn);

setDate();
setTime();
setPages();

setInterval(setDate, 3000);
document.addEventListener('visibilitychange', function() {
    setColor();
    setDate();
    setTime();
});
setInterval(setTime, 1500);